import axios from "axios";
import Vue from "vue";
import { Loading } from "element-ui";

let loadingInstance;
//请求拦截器
axios.interceptors.request.use(
  (config) => {
    loadingInstance = Loading.service({
      fullscreen: true,
      lock: true,
      text: "正在加载...",
      background: "rgba(0,0,0,.7)",
    });
    let token = sessionStorage.getItem("token");
    if (token) {
      config.headers.Authorization = token;
    }

    return config;
  },
  function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  }
);

//响应拦截器
axios.interceptors.response.use(
  (res) => {
    loadingInstance.close();
    return res;
  },
  function (error) {
    // 超出 2xx 范围的状态码都会触发该函数。
    // 对响应错误做点什么
    return Promise.reject(error);
  }
);

axios.defaults.baseURL = "/hhh/";
Vue.prototype.$axios = axios;
